import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { WinRabbitMoneyStrategyV2 } from "./WinRabbitMoneyStrategy";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { NormalGroupMoneyStrategy } from "./NormalGroupMoneyStrategy";
import { WisdomGroupMoneyStrategyV2 } from "./WisdomMoneyStrategyV2";
import { BossRabbitMoneyStrategy, BossRabbitToStepMoneyStrategy, BossThreeRabiatMoneyStrategy, MaxBossRabbitMoneyStrategy } from "./BossRabitMoneyStrategy";
import { WithRecmMoneyStrategy } from "./WithRecmMoneyStrategy";
import { MaxStepMoneyhStrategy, NormalStepMoneyStrategy } from "./NormalStepMoneyStrategy";
import { WisdomMoneyStrategyV29Group4 } from "./WisdomMoneyStrategyV29Group4";
import { WisdomMoneyStrategyV29Group4V2 } from "./WisdomMoneyStrategyV29Group4V2";
import { WisdomMoneyStrategyV29Group4V3 } from "./WisdomMoneyStrategyV29Group4V3";
import { WisdomMoneyStrategyV29Group4V5 } from "./WisdomMoneyStrategyV29Group4V5";


export class WisdomMoneyStrategyV29_5 implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV29Debug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 10000;
    protected _subPlanSum: number = 0;
    public get subPlanSum() {
        return this._subPlanSum;
    }
    constructor(name?: string) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;


        this.subPlan.forEach(pl => {
            ;
            let [rv, rm] = pl.showMoney();
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;

            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += (pl.recmInfo.recmMoney * pl.multiple);

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += (pl.recmInfo.recmMoney * pl.multiple);
            }
        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }


        return [recmV, recmMoney];
    }
    resetRecm() {
        this.subPlan.forEach(item => item.resetRecm());
    }
    resetStrategy(): void {
        this._subPlanSum = 0;

        let flPlan = new ExcutePlan();
        flPlan.planName = "复利";
        flPlan.baseMoney = 10;
        flPlan.recmStrategy = [new FixedStrategy({ reverse: false })]
        flPlan.moneyStrategy = [new NormalGroupMoneyStrategy("", {
            groupCount: 1,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            maxLevel: 1000000000
        })];
        flPlan.toResult(new RoundInfo());
        //正向，兔子算法
        let f_rabbit_plan = new ExcutePlan();
        f_rabbit_plan.multiple = 0;
        f_rabbit_plan.planName = "正兔";
        f_rabbit_plan.recmStrategy = [new FixedStrategy({ reverse: false })];
        f_rabbit_plan.moneyStrategy = [new BossRabbitMoneyStrategy("方案一", {
            baseMoney: 10,
            winStep: 1,
            failStep: 0,
            currentMoney: 10,
            prevMoney: 0
        })];
        f_rabbit_plan.toResult(new RoundInfo());

        //反向，兔子算法
        let p_rabbit_plan = new ExcutePlan();
        p_rabbit_plan.multiple = 0;
        p_rabbit_plan.planName = "反兔";
        p_rabbit_plan.recmStrategy = [new FixedStrategy({ reverse: true })]
        p_rabbit_plan.moneyStrategy = [new BossRabbitMoneyStrategy("方案二", {
            baseMoney: 10,
            winStep: 1,
            failStep: 0,
            currentMoney: 10,
            prevMoney: 0
        })];
        p_rabbit_plan.toResult(new RoundInfo());

        let tz = new ExcutePlan();
        tz.multiple = 2;
        tz.planName = "兔正";
        tz.recmStrategy = [new FixedStrategy({ reverse: false })];
        tz.moneyStrategy = [new BossRabbitMoneyStrategy("方案三", {
            baseMoney: 10,
            winStep: -2,
            failStep: 1,
            currentMoney: 10,
            prevMoney: 0,
            maxMoney: 2330,
        })];
        tz.toResult(new RoundInfo());


        let tf = new ExcutePlan();
        tf.planName = "兔反";
        tf.multiple = 2;
        tf.recmStrategy = [new FixedStrategy({ reverse: true })];
        tf.moneyStrategy = [new BossRabbitMoneyStrategy("方案三", {
            baseMoney: 10,
            winStep: -2,
            failStep: 1,
            currentMoney: 10,
            prevMoney: 0,
            maxMoney: 2330,
        })];
        tf.toResult(new RoundInfo());


        let v29GroupV2 = new ExcutePlan();
        v29GroupV2.planName = "组合V5";
        v29GroupV2.recmStrategy = [new FixedStrategy({ reverse: false })]
        v29GroupV2.moneyStrategy = [new WisdomMoneyStrategyV29Group4V5("组合V5")];

        this.subPlan = [f_rabbit_plan, p_rabbit_plan,
            v29GroupV2,
        ];

        let gi = new ExcutePlan();
        gi.planName = `2正2`;
        gi.baseMoney = 10;
        gi.recmStrategy = [new FixedStrategy({ reverse: false })];
        gi.relationPindex = [this.subPlan.length + 1];
        gi.moneyStrategy = [new WisdomGroupMoneyStrategyV2(`$wisdom-group-1`, {
            groupCount: 2,
            addStep: 10,
            devideStep: -10,
            reverse: false, //true:赢了+100，输了-100
            maxLevel: 300000000,
        })]
        gi.toResult(new RoundInfo());
        //this.subPlan.push(gi);

        let gif = new ExcutePlan();
        gif.planName = `2反2`;
        gif.baseMoney = 10;
        gif.relationPindex = [this.subPlan.length - 1];
        gif.recmStrategy = [new FixedStrategy({ reverse: true })];
        gif.moneyStrategy = [new WisdomGroupMoneyStrategyV2(`$reverse-wisdom-group-1`, {
            groupCount: 2,
            addStep: 10,
            devideStep: -10,
            reverse: false, //true:赢了+100，输了-100
            maxLevel: 300000000
        })]
        gif.toResult(new RoundInfo());
        //this.subPlan.push(gif);

        //this.subPlan.push(flPlan);

        var pgPlan1 = new ExcutePlan();
        pgPlan1.planName = "复正2";
        pgPlan1.recmStrategy = [new FixedStrategy({ reverse: false })];
        pgPlan1.moneyStrategy = [new NormalStepMoneyStrategy("复正2", {
            baseMoney: 10,
            startMoney: 10,
            addStep: -10,
            devideStep: 10,
            reverse: false,
            resetLevel: 80,
        }, true)];
        pgPlan1.toResult(new RoundInfo());
        // this.subPlan.push(pgPlan1);

        var pgfPlan1 = new ExcutePlan();
        pgfPlan1.planName = "复反2";
        pgfPlan1.recmStrategy = [new FixedStrategy({ reverse: true })];
        pgfPlan1.moneyStrategy = [new NormalStepMoneyStrategy("复反2", {
            baseMoney: 10,
            startMoney: 10,
            addStep: -10,
            devideStep: 10,
            reverse: false,
            resetLevel: 80,
        }, true)];
        pgfPlan1.toResult(new RoundInfo());
        //this.subPlan.push(pgfPlan1);

    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sumWin = 0;


        let sum = 0;
        this.subPlan.forEach(pl => {
            pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);

            let userRecmV = pl.toResult(roundInfo);
            let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
            pl.userRecmInfo.recmV = userRecmV;
            let [rv, rm] = pl.toMoney(newRoundInfo, pl.recmInfo, pl.userRecmInfo, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;

            if (pl.moneyStrategy[0] instanceof WisdomMoneyStrategyV29Group4) {
                sum += (pl.moneyStrategy[0] as WisdomMoneyStrategyV29Group4).subPlanSum;
            }
            else {
                let m = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;
                sum += m;
            }

            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += (pl.recmInfo.recmMoney * pl.multiple);

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += (pl.recmInfo.recmMoney * pl.multiple);
            }
            if (rv != EnumBjlResult.U) {
                //pl.recmInfo.
                pl.recmInfo.betCount++;
            }
        });
        this._subPlanSum = sum;
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }


        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV29_5;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            this._subPlanSum = obj._subPlanSum;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}